/[admin]/admin2/client/widgets/admin_color.lua

https://github.com/multitheftauto/mtasa-resources · Lua · 277 lines · 249 code · 18 blank · 10 comment · 40 complexity · 337359ade2abb38490590159e7c7fb08 MD5 · raw file

  1. --[[**********************************
  2. *
  3. * Multi Theft Auto - Admin Panel
  4. *
  5. * client\widgets\admin_color.lua
  6. *
  7. * Original File by lil_Toady
  8. *
  9. **************************************]]
  10. aColor = {
  11. Form = nil,
  12. Color = {
  13. r = 0,
  14. g = 0,
  15. b = 0,
  16. h = 0,
  17. s = 0
  18. },
  19. Picking = false,
  20. Thread = nil
  21. }
  22. function aColor.Open(x, y, r, g, b, relative, parent)
  23. local sx, sy = guiGetScreenSize()
  24. if (not x or not y) then
  25. x, y = getCursorPosition()
  26. x = sx * x
  27. y = sy * y
  28. else
  29. if (relative) then
  30. if (parent) then
  31. local px, py = guiGetSize(parent, false)
  32. x = px * x
  33. x = py * y
  34. else
  35. x = sx * x
  36. y = sy * y
  37. end
  38. end
  39. if (parent) then
  40. while (parent ~= nil) do
  41. local px, py = guiGetPosition(parent, false)
  42. x = px + x
  43. y = py + y
  44. parent = getElementParent(parent)
  45. end
  46. end
  47. end
  48. x = x - 1
  49. y = y - 1
  50. if (r and g and b) then
  51. aColor.Color.r = math.floor(r) % 256
  52. aColor.Color.g = math.floor(g) % 256
  53. aColor.Color.b = math.floor(b) % 256
  54. else
  55. aColor.Color = {r = 255, g = 0, b = 0}
  56. end
  57. aColor.Color.h, aColor.Color.s = aColor.rgb2hs(aColor.Color.r, aColor.Color.g, aColor.Color.b)
  58. if (not aColor.Form) then
  59. aColor.Form = guiCreateStaticImage(x, y, 210, 138, "client/images/black.png", false)
  60. guiSetAlpha(aColor.Form, 0.6)
  61. aColor.Palette = guiCreateStaticImage(5, 5, 128, 128, "client/images/palette.png", false, aColor.Form)
  62. guiCreateLabel(138, 37, 10, 20, "R:", false, aColor.Form)
  63. guiCreateLabel(138, 57, 10, 20, "G:", false, aColor.Form)
  64. guiCreateLabel(138, 77, 10, 20, "B:", false, aColor.Form)
  65. aColor.R = guiCreateEdit(155, 35, 50, 20, "", false, aColor.Form)
  66. aColor.G = guiCreateEdit(155, 55, 50, 20, "", false, aColor.Form)
  67. aColor.B = guiCreateEdit(155, 75, 50, 20, "", false, aColor.Form)
  68. aColor.Ok = guiCreateButton(155, 113, 50, 20, "ok", false, aColor.Form)
  69. guiSetProperty(aColor.Form, "AlwaysOnTop", "true")
  70. aRegister("Color", aColor.Form, aColor.Open, aColor.Close)
  71. end
  72. guiSetText(aColor.R, tostring(aColor.Color.r))
  73. guiSetText(aColor.G, tostring(aColor.Color.g))
  74. guiSetText(aColor.B, tostring(aColor.Color.b))
  75. aColor.Picking = false
  76. guiSetVisible(aColor.Form, true)
  77. addEventHandler("onClientRender", getRootElement(), aColor.onRender)
  78. addEventHandler("onClientGUIChanged", aColor.Form, aColor.onChanged)
  79. addEventHandler("onClientGUIBlur", aColor.Form, aColor.onBlur)
  80. setTimer(
  81. function()
  82. -- some hack for window not to get insta closed if opened in click handler
  83. if (aColor.Form and guiGetVisible(aColor.Form)) then
  84. guiBringToFront(aColor.Form)
  85. addEventHandler("onClientClick", getRootElement(), aColor.onClick)
  86. end
  87. end,
  88. 50,
  89. 1
  90. )
  91. aColor.Thread = sourceCoroutine
  92. coroutine.yield()
  93. aColor.Thread = nil
  94. return aColor.Color.r, aColor.Color.g, aColor.Color.b
  95. end
  96. function aColor.Close(destroy)
  97. guiSetInputEnabled(false)
  98. if (aColor.Form) then
  99. removeEventHandler("onClientGUIBlur", aColor.Form, aColor.onBlur)
  100. removeEventHandler("onClientGUIChanged", aColor.Form, aColor.onChanged)
  101. removeEventHandler("onClientClick", getRootElement(), aColor.onClick)
  102. removeEventHandler("onClientRender", getRootElement(), aColor.onRender)
  103. if (destroy) then
  104. destroyElement(aColor.Form)
  105. aColor.Form = nil
  106. else
  107. guiSetVisible(aColor.Form, false)
  108. end
  109. if (aColor.Thread) then
  110. coroutine.resume(aColor.Thread)
  111. end
  112. end
  113. end
  114. function aColor.onClick(button, state, x, y)
  115. local px, py = guiGetPosition(aColor.Form, false)
  116. if (state == "up") then
  117. if (aColor.Picking) then
  118. aColor.Picking = false
  119. return
  120. end
  121. local sx, sy = guiGetSize(aColor.Form, false)
  122. if (x < px or x > px + sx) or (y < py or y > py + sy) then
  123. aColor.Close()
  124. return
  125. end
  126. end
  127. if (button ~= "left") then
  128. return
  129. end
  130. if (x >= px + 5 and x <= px + 133) and (y >= py + 5 and y <= py + 133) then
  131. aColor.Picking = state == "down"
  132. end
  133. end
  134. function aColor.onRender()
  135. if (isConsoleActive()) then
  136. return
  137. end
  138. local color = aColor.Color
  139. local x, y = guiGetPosition(aColor.Form, false)
  140. x = x + 5
  141. y = y + 5
  142. if (aColor.Picking) then
  143. local sx, sy = guiGetScreenSize()
  144. local cx, cy = getCursorPosition()
  145. cx = sx * cx
  146. cy = sy * cy
  147. if (cx < x) then
  148. cx = x
  149. elseif (cx > x + 127) then
  150. cx = x + 127
  151. end
  152. if (cy < y) then
  153. cy = y
  154. elseif (cy > y + 127) then
  155. cy = y + 127
  156. end
  157. color.h, color.s = (cx - x) / 127, (127 - cy + y) / 127
  158. color.r, color.g, color.b = aColor.hs2rgb(color.h, color.s)
  159. guiSetText(aColor.R, tostring(color.r))
  160. guiSetText(aColor.G, tostring(color.g))
  161. guiSetText(aColor.B, tostring(color.b))
  162. end
  163. dxDrawLine(x + 133, y + 10, x + 200, y + 10, tocolor(color.r, color.g, color.b, 255), 20, true)
  164. x = x + color.h * 127
  165. y = y + (1 - color.s) * 127
  166. local c = tocolor(0, 0, 0, 255)
  167. dxDrawLine(x - 7, y, x - 2, y, c, 2, true)
  168. dxDrawLine(x + 2, y, x + 7, y, c, 2, true)
  169. dxDrawLine(x, y - 7, x, y - 2, c, 2, true)
  170. dxDrawLine(x, y + 2, x, y + 7, c, 2, true)
  171. end
  172. function aColor.onChanged()
  173. local acc = {[aColor.R] = "r", [aColor.G] = "g", [aColor.B] = "b"}
  174. if (acc[source]) then
  175. local value = tonumber(guiGetText(source))
  176. if (not value) then
  177. if (guiGetText(source) == "") then
  178. aColor.Color[acc[source]] = 0
  179. else
  180. guiSetText(source, aColor.Color[acc[source]])
  181. end
  182. elseif (value >= 0 and value <= 255) then
  183. aColor.Color[acc[source]] = value
  184. else
  185. guiSetText(source, aColor.Color[acc[source]])
  186. end
  187. aColor.Color.h, aColor.Color.s = aColor.rgb2hs(aColor.Color.r, aColor.Color.g, aColor.Color.b)
  188. end
  189. end
  190. function aColor.onBlur()
  191. local acc = {[aColor.R] = "r", [aColor.G] = "g", [aColor.B] = "b"}
  192. if (acc[source]) then
  193. if (guiGetText(source) == "") then
  194. guiSetText(source, "0")
  195. end
  196. end
  197. end
  198. function aColor.hs2rgb(h, s)
  199. local m2 = (0.5 + s) - (0.5 * s)
  200. local m1 = 1 - m2
  201. local r = aColor.hue2rgb(m1, m2, h + 1 / 3)
  202. local g = aColor.hue2rgb(m1, m2, h)
  203. local b = aColor.hue2rgb(m1, m2, h - 1 / 3)
  204. return math.floor(r * 255), math.floor(g * 255), math.floor(b * 255)
  205. end
  206. function aColor.hue2rgb(m1, m2, h)
  207. if (h < 0) then
  208. h = h + 1
  209. elseif (h > 1) then
  210. h = h - 1
  211. end
  212. if h * 6 < 1 then
  213. return m1 + (m2 - m1) * h * 6
  214. elseif h * 2 < 1 then
  215. return m2
  216. elseif h * 3 < 2 then
  217. return m1 + (m2 - m1) * (2 / 3 - h) * 6
  218. else
  219. return m1
  220. end
  221. end
  222. function aColor.rgb2hs(r, g, b)
  223. local max = math.max(r, g, b)
  224. local min = math.min(r, g, b)
  225. local l = (min + max) / 2
  226. local h = 0
  227. local s = 0
  228. if (max ~= min) then
  229. local d = max - min
  230. if l < 0.5 then
  231. s = d / (max + min)
  232. else
  233. s = d / (2 - max - min)
  234. end
  235. if max == r then
  236. h = (g - b) / d
  237. if g < b then
  238. h = h + 6
  239. end
  240. elseif max == g then
  241. h = (b - r) / d + 2
  242. else
  243. h = (r - g) / d + 4
  244. end
  245. h = h / 6
  246. end
  247. return h, -s
  248. end